package com.zerui.manager.mq;

import com.zerui.manager.common.mq.consumer.DeviceMessage;
import com.zerui.manager.web.AccessAuthDeviceService;
import com.zerui.manager.web.DeviceService;
import com.zerui.manager.web.OpenPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author wh
 * @create_time 2023-05-11 17:10
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}-device",topic = "device-topic",messageModel = MessageModel.BROADCASTING)
public class DeviceRocketMqConsumer implements RocketMQListener<DeviceMessage> {

    @Resource
    private AccessAuthDeviceService accessAuthDeviceService;

    @Resource
    private OpenPlanService openPlanService;

    @Override
    public void onMessage(DeviceMessage deviceMessage) {
        log.info("device delete or update message handle start....,{}",deviceMessage);
        if(ObjectUtils.isEmpty(deviceMessage) || StringUtils.isBlank(deviceMessage.getDeviceId())){
            return;
        }
        Long deviceId = Long.parseLong(deviceMessage.getDeviceId());
        Integer operate = deviceMessage.getOperate();
        if(operate.equals(0) || operate.equals(1)){
            accessAuthDeviceService.deleteDeviceById(deviceId);
            openPlanService.deleteDeviceById(deviceId);
        }else {
            accessAuthDeviceService.sync();
        }

        log.info("device delete or update message handle end....");

    }
}
